'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE } = Sequelize;
    
    await queryInterface.createTable('product_categories', {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      name: {
        type: STRING(100),
        allowNull: false,
        comment: '类目名称'
      },
      parent_id: {
        type: INTEGER,
        allowNull: true,
        comment: '父类目ID，顶级类目为null'
      },
      level: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: '类目层级：1-一级类目，2-二级类目，3-三级类目，4-四级类目'
      },
      sort: {
        type: INTEGER,
        defaultValue: 0,
        comment: '排序值，越小越靠前'
      },
      status: {
        type: STRING(20),
        defaultValue: 'active',
        comment: '类目状态: active-启用, inactive-禁用'
      },
      created_at: {
        type: DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      }
    });
    
    // 添加索引
    await queryInterface.addIndex('product_categories', ['parent_id'], {
      name: 'product_categories_idx_parent_id',
      unique: false
    });
    await queryInterface.addIndex('product_categories', ['level'], {
      name: 'product_categories_idx_level',
      unique: false
    });
    await queryInterface.addIndex('product_categories', ['status'], {
      name: 'product_categories_idx_status',
      unique: false
    });
  },

  down: async (queryInterface, Sequelize) => {
    // 删除索引
    await queryInterface.removeIndex('product_categories', 'product_categories_idx_parent_id');
    await queryInterface.removeIndex('product_categories', 'product_categories_idx_level');
    await queryInterface.removeIndex('product_categories', 'product_categories_idx_status');
    
    await queryInterface.dropTable('product_categories');
  }
};